import 'package:flutter/material.dart';
import 'package:flutter_common/widget/funAppBar.dart';
import 'package:flutter_fun_app/utils/setting.dart';
import 'package:flutter_fun_app/utils/eventBus.dart';

class LangSelectPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return LangSelectState();
  }
}

class LangSelectState extends State<LangSelectPage> {
  String _newValue;
  var bus = new EventBus();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    String lang = Setting.getLang();
    if (lang.isEmpty) {
      lang = Setting.ZH;
    }
    setState(() {
      _newValue = lang;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: FunAppBar(
        title: "选择语言",
      ),
      body: Column(
        children: <Widget>[
          RadioListTile<String>(
            value: 'zh',
            title: Text('中文'),
            groupValue: _newValue,
            onChanged: (value) {
              setState(() {
                _newValue = value;
                Setting.setLang(value);
                bus.emit('changeLang', Locale('zh', 'CN'));
              });
            },
          ),
          RadioListTile<String>(
            value: 'en',
            title: Text('English'),
            groupValue: _newValue,
            onChanged: (value) {
              setState(() {
                _newValue = value;
                Setting.setLang(value);
                bus.emit('changeLang', Locale('en', ''));
              });
            },
          ),
        ],
      ),
    );
  }
}
